package com.pany.rememberpass;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.pany.rememberpass.Adapter.MyAdapter;
import com.pany.rememberpass.bean.ItemBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RecyclerViewActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private List<ItemBean> myList;


    private int[] iamges = {
            R.drawable.test8,
            R.drawable.test2,
            R.drawable.test6,
            R.drawable.test5,
            R.drawable.test4,
            R.drawable.test7,
            R.drawable.test3,
            R.drawable.test1,
            R.drawable.test9
    };
    private String[] strings = {
            " 怕康纳是",
            " bzdgrds ",
            " 不知道该色",
            " 大哥啊让一个个",
            " 安慰她企鹅号多少分贝",
            " 安徽人收到退回给",
            " 的故事的",
            " 爱人和高尚的活动空间可以就的人格",
            " 的故事大纲"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {

        MyAdapter myAdapter = new MyAdapter(this, myList);
        mRecyclerView.setAdapter(myAdapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this ,RecyclerView.HORIZONTAL,false);
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this ,
//                RecyclerView.VERTICAL,true);// 倒序排列
//        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.VERTICAL);

        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);
    }

    private void initData() {

        String textContent = "111";

        Random random = new Random(strings.length);

        for (int i = 0; i < 50; i++) {


            ItemBean itemBean = new ItemBean();
            itemBean.setTitle("这是标题" + strings[i % strings.length] + i);
            for (int j = 0; j < strings.length; j++) {
                int nextInt = random.nextInt(strings.length);
                textContent = strings[nextInt] + textContent;
            }
            itemBean.setContent("这是内容" + textContent + i);
            textContent = "";


            itemBean.setImgRId(iamges[i % iamges.length]);

            myList.add(itemBean);
        }
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.rlv);
        myList = new ArrayList();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_linlayout:
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
                mRecyclerView.setLayoutManager(linearLayoutManager);
                break;
            case R.id.item_girdlayiyt:
                GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);

                mRecyclerView.setLayoutManager(gridLayoutManager);
                break;
            case R.id.item_stagglayout:

                StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,
                        StaggeredGridLayoutManager.VERTICAL);

                mRecyclerView.setLayoutManager(staggeredGridLayoutManager);

                break;
        }

        return super.onOptionsItemSelected(item);
    }
}